1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package com.google.common.hash;
16
17 import static com.google.common.base.Preconditions.checkArgument;
18 import static com.google.common.base.Preconditions.checkNotNull;
19
20 import com.google.common.base.Supplier;
21
22 import java.io.Serializable;
23 import java.util.zip.Checksum;
24
25
26
27
28
29
30 final class ChecksumHashFunction extends AbstractStreamingHashFunction implements Serializable {
31
32 private final Supplier<? extends Checksum> checksumSupplier;
33 private final int bits;
34 private final String toString;
35
36 ChecksumHashFunction(Supplier<? extends Checksum> checksumSupplier, int bits, String toString) {
37 this.checksumSupplier = checkNotNull(checksumSupplier);
38 checkArgument(bits == 32 || bits == 64, "bits (%s) must be either 32 or 64", bits);
39 this.bits = bits;
40 this.toString = checkNotNull(toString);
41 }
42
43 @Override
44 public int bits() {
45 return bits;
46 }
47
48 @Override
49 public Hasher newHasher() {
50 return new ChecksumHasher(checksumSupplier.get());
51 }
52
53 @Override
54 public String toString() {
55 return toString;
56 }
57
58
59
60
61 private final class ChecksumHasher extends AbstractByteHasher {
62
63 private final Checksum checksum;
64
65 private ChecksumHasher(Checksum checksum) {
66 this.checksum = checkNotNull(checksum);
67 }
68
69 @Override
70 protected void update(byte b) {
71 checksum.update(b);
72 }
73
74 @Override
75 protected void update(byte[] bytes, int off, int len) {
76 checksum.update(bytes, off, len);
77 }
78
79 @Override
80 public HashCode hash() {
81 long value = checksum.getValue();
82 if (bits == 32) {
83
84
85
86
87
88 return HashCode.fromInt((int) value);
89 } else {
90 return HashCode.fromLong(value);
91 }
92 }
93 }
94
95 private static final long serialVersionUID = 0L;
96 }